package com.attack.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attack.api.annotation.ExtApiIdempotent;
import com.attack.api.utils.TokenUtils;
import com.attack.entity.UserEntity;
import com.attack.mapper.UserMapper;

@Controller
@RequestMapping("/redis")
public class TokenRedisController {
	@Autowired
	private TokenUtils tokenUtils;
	@Autowired
	private UserMapper userMapper;
	@RequestMapping("/getToken")
	@ResponseBody
	public String getToken(){
		return tokenUtils.getToken();
	}
	
	// 验证Token
		@RequestMapping(value = "/addOrder", produces = "application/json; charset=utf-8")
		@ResponseBody
		@ExtApiIdempotent(type="head")
		public String addOrder(UserEntity userEntity, HttpServletRequest request) {
		/*	// 代码步骤：
			// 1.获取令牌 存放在请求头中
			String token = request.getHeader("token");
			if (StringUtils.isEmpty(token)) {
				return "参数错误!";
			}
			// 2.判断令牌是否在缓存中有对应的令牌
			// 3.如何缓存没有该令牌的话，直接报错（请勿重复提交）
			// 4.如何缓存有该令牌的话，直接执行该业务逻辑
			// 5.执行完业务逻辑之后，直接删除该令牌。
			if (!tokenUtils.findToken(token)) {
				return "请勿重复提交!";
			}*/
			int result = userMapper.insertUser(userEntity);
			return result > 0 ? "添加成功" : "添加失败" + "";
		}
}
